package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.cart.CartInfo;
import com.atguigu.gmall.model.cart.CartInfoVo;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author 钟涛
 * @version 1.0
 * @description: TODO
 * @date 2021/12/23 20:36
 */
@RestController
@RequestMapping("/api/cart")
public class CartRestController {
    @Autowired
   private CartService cartService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @GetMapping("/cartList")
    public Result cartList(@RequestHeader(value = "userId",required = false) String userId,
                           @RequestHeader(value = "userTempId",required = false) String userTempId) throws JsonProcessingException {
       String cartKey = cartService.determinCartKey(userId,userTempId);
       List<CartInfo> cartInfoList = cartService.getCartList(cartKey);


       //是否需要合并购物车
        cartService.mergeCart(userId,userTempId);
        if(StringUtils.isEmpty(userId)){
            redisTemplate.expire(cartKey,365, TimeUnit.DAYS);
        }
        return Result.ok(cartInfoList);
    }
    @PostMapping("/addToCart/{skuId}/{num}")
    public Result updateSkuNum(@PathVariable("skuId") Long skuId,
                               @PathVariable("num") Integer num,
                               @RequestHeader(value = "userId",required = false) String userId,
                               @RequestHeader(value = "userTempId",required = false) String userTempId) throws JsonProcessingException {

        String cartKey = cartService.determinCartKey(userId,userTempId);
        cartService.updateSkuNum(cartKey,skuId,num);
        if(StringUtils.isEmpty(userId)){
            redisTemplate.expire(cartKey,365, TimeUnit.DAYS);
        }
        return Result.ok();
    }

    @GetMapping("/checkCart/{skuId}/{status}")
    public Result updateSkuStatus(@PathVariable("skuId") Long skuId,
                                  @PathVariable("status") Integer status,
                                  @RequestHeader(value = "userId",required = false) String userId,
                                  @RequestHeader(value = "userTempId",required = false) String userTempId) throws JsonProcessingException {

        String cartKey = cartService.determinCartKey(userId, userTempId);
        cartService.updateSkuStatus(cartKey,skuId,status);

        if(StringUtils.isEmpty(userId)){
            redisTemplate.expire(cartKey,365, TimeUnit.DAYS);
        }

        return Result.ok();
    }


   @DeleteMapping("/deleteCart/{skuId}")
   public Result deleteCartInfo(@PathVariable("skuId") Long skuId,
                                @RequestHeader(value = "userId",required = false) String userId,
                                @RequestHeader(value = "userTempId",required = false) String userTempId){
       String cartKey = cartService.determinCartKey(userId, userTempId);
       cartService.deleteCartInfo(cartKey,skuId);

       if(StringUtils.isEmpty(userId)){
           redisTemplate.expire(cartKey,365, TimeUnit.DAYS);
       }

       return Result.ok();
   }
}
